<?php
namespace app\blogadmin\controller;

use app\blogadmin\model\Power as powerM;
use think\Db;
use think\Request;

class Power extends Base{
    /**
     * 管理员列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lst(){
        $powerM=new powerM();
        $res=$powerM->getAllpower();
        $res=loop($res);
        $this->assign('power',$res);
        return $this->fetch();
    }


    /**
     * 添加管理员
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        //获取权限列表
        $powerL=Db::table('admin_power')->where('status=1')->select();
        $powerLs =loop($powerL);
        if(Request::instance()->isPost()){
            //获取提交的数据，实体化数据
            $data=input("post.");
            $info['pid']=htmlspecialchars($data['pid']);
            $info['name']=htmlspecialchars($data['name']);
            $info['url']=htmlspecialchars($data['url']);
            $info['status']=htmlspecialchars($data['status']);
            $res=Db::table('admin_power')->insert($info);
            if($res){
                return json(['msg'=>'添加数据成功','code'=>'1']);
            }else {
                $this->error('');
                return json(['msg' => '添加数据失败', 'code' => '-1']);
            }
        }
        $this->assign('powerL',$powerLs);

        return $this->fetch();
    }

    /**
     * 修改管理员
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit(){
        $id=Request::instance()->param('id');//获取当前id
        $cur_power=Db::table('admin_power')->find(['id'=>$id]); //获取当前用户信息
        //获取权限列表
        $powerL=Db::table('admin_power')->where('status=1')->select();
        $powerLs =loop($powerL);
       if(Request::instance()->isPost()){
           $data=input("post.");
           $info['id']=htmlspecialchars($data['id']);
           $info['pid']=htmlspecialchars($data['pid']);
           $info['name']=htmlspecialchars($data['name']);
           $info['url']=htmlspecialchars($data['url']);
           $info['status']=htmlspecialchars($data['status']);

            $res=Db::table('admin_power')->update($info);
            if($res){
                return json(['code'=>1,'msg'=>"修改数据成功"]);
            }else{
                return json(['code'=>-1,'msg'=>"修改数据失败"]);
            }
       }
       $this->assign('cur_power',$cur_power);
       $this->assign('powerL',$powerLs);
       return $this->fetch();
    }

    /**
     * 删除权限
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(){
        if(Request::instance()->isPost()){
            $id=input('post.id');
            //获取所有的权限数据
            $data=Db::table('admin_power')->where('status=1')->select();

            //向下查找是否存在子级的权限
            $childInfo=getChildInfo($id,$data);
            foreach ($childInfo as $key=>$value){
                $idArr=array_push($value['id']);
            }

            dump($idArr);die;

            dump($childInfo);die;

            $res=Db::table('admin_power')->delete($id);
            if($res) {
                return json(['code'=>'1','msg'=>'删除数据成功']);
            }else{
                return json(['code'=>'0','msg'=>'删除数据失败']);
            }
        }
    }





    public function sss(){
        return $this->fetch();
    }



}